<?php
    namespace app\controllers;
    use app\common\BaseController;
    use app\common\Utils;
    use app\models\ChargeTypes;
    use app\models\Product;
    use app\models\Store;

    class StoreController extends BaseController{
        public function actionAdd(){
            $postData = \Yii::$app->request->post();
            if (isset($postData['store'])){
                $storeInfo = json_decode($postData['store'], true);
                $storeInfo['longitude'] = $storeInfo['longitude'].'';
                $storeInfo['latitude'] = $storeInfo['latitude'].'';
                if (isset($storeInfo['id'])){
                    $store = Store::find()->where(['id' => $storeInfo['id']])->one();
                }else{
                    $store = new Store();
                }

                if ($store->load($storeInfo, '') && $store->save()){
                    if (isset($postData['productList'])){
                        $productList = json_decode($postData['productList'], true);
                        foreach ($productList as $key => $value){
                            if (isset($value['id'])){
                                $product = Product::find()->where(['id' => $value['id']])->one();
                            }else{
                                $product  = new Product();
                            }

                            $value['storeId'] = $store->id;
                            $value['disabled'] = 0;
                            $value['price'] = floatval($value['price']);
                            if (!$product->load($value, '') || !$product->save()){
                                return $this->result([], 1, $product->errors);
                            }
                        }
                    }
                }else{
                    return $this->result([], 1, $store->errors);
                }
            }
            return $this->result([]);
        }

        public function actionList(){
            $res = Store::find()->asArray()->all();
            return $this->result($res);
        }

        public function actionGetDetail(){
            $id = \Yii::$app->request->get('id', 0);
            $result = [];
            if ($id){
                $storeInfo = Store::find()->where(['id' => $id])->asArray()->one();
                $productList = Product::find()->where(['storeId' => $id])->asArray()->all();
                $typeInfo = ChargeTypes::find()->where(['id' => $storeInfo['type']])->asArray()->one();
                $result = [
                    'storeInfo' => $storeInfo,
                    'productList' => $productList,
                    'typeInfo' => $typeInfo
                ];
            }
            return $this->result($result);
        }

        public function actionGetStoreByTypeId(){
            $id = \Yii::$app->request->get('typeId', 0);
            $result = [];
            if ($id){
                $result = Store::find()->where(['type' => $id])->asArray()->all();
            }
            return $this->result($result);
        }

        public function actionGetProductByStoreId(){
            $id = \Yii::$app->request->get('storeId', 0);
            $result = [];
            if ($id){
                $result = Product::find()->where(['storeId' => $id])->asArray()->all();
            }
            return $this->result($result);
        }

        public function actionGetStoreTree(){
            $allTypes = ChargeTypes::find()->where(['pid' => 0])->select(['id', 'text'])->asArray()->all();
            foreach ($allTypes as $key => $value){
                $posts = \Yii::$app->db->createCommand("WITH recursive cte (id, text) AS (
                        SELECT id,text from chargeTypes where id={$value['id']}
                        UNION
                        SELECT chargeTypes.id,chargeTypes.text from cte, chargeTypes where chargeTypes.pid=cte.id
                    )
                    SELECT * from cte;")
                    ->queryAll();
                array_shift($posts);
                $allTypes[$key]['children'] = [];

                $flag = 0;
                foreach ($posts as $kk => $vv){
                     $allStore = Store::find()->where(['type' => $vv['id']])->select(['id', 'name as text'])->asArray()->all();

                     if (count($allStore)){
                         $posts[$kk]['children'] = $allStore;
                         array_push($allTypes[$key]['children'], $posts[$kk]);
                         $flag = 1;
                     }
                }
                if ($flag === 0){
                    unset($allTypes[$key]);
                }
            }
            return $this->result($allTypes);
        }
    }